﻿using System;
using System.Data ;
using System.Linq;
using LayerEntity.Common ;

namespace LayerEntity.Compras
{ 
    public class EDetalleCompra
    {
        #region Variables
        double dblSutTotal ;
        double dblSubTotalSinIng;
        double dblPorcentajeIgv;
        double dblDescuento;
        #endregion

        public EDetalleCompra()
        {
            this.Item = 0;
            this.IdSucursal = -1;
            this.NumeroFactura = "";
            this.IdDireccion = -1;
            this.IdProveedor = "";
            this.FechaFactura = DateTime.Now ;
            this.IdUnidad = -1;
            this.IdProducto = "";
            this.IdLista = -1;
            this.IdLote = "";
            this.IdAlmacen = -1;
            this.IdPrecio = -1;
            this.Precio = 0.0;
            this.Cantidad = 0.0;
            this.SubTotal = 0.0;
            //this.PorcentajeIgv = 0.0;
            dblPorcentajeIgv = 0.0;
            this.Igv = 0.0;
            this.SubTotalSinIgv = 0.0;
            this.Flete = 0.0;
            this.Peso = 0.0;
            //this.Descuento = 0.0;
            dblDescuento = 0.0;
            this.PorcentajeDescuento = 0.0;
            this.FechaRegistro = DateTime .Now ;
            this.EstadoRow = EnumRowState .Insertar ;
        }

        public EDetalleCompra(IDataRecord record)
        {
            this.Item = (int) record["item"];
            this.IdSucursal = (int) record["id_sucursal"];
            this.NumeroFactura = record["nro_doc_compra"].ToString();
            this.IdDireccion = (int) record["id_dir_proveedor"];
            this.IdProveedor = record["id_proveedor"].ToString();
            this.FechaFactura = (DateTime) record["fec_documento"];
            this.IdUnidad = (int) record["id_unidad"];
            this.IdProducto = record["id_producto"].ToString();
            this.IdLista = (int) record["id_lista"];
            this.IdLote = record["id_lote"].ToString();
            this.IdAlmacen = (int) record["id_almacen"];
            this.IdPrecio = (int) record["id_precios"];
            this.Precio = (double )record["precio"];
            this.Cantidad = (double )record["cantidad"];
            this.SubTotal = (double )record["incluido"];
            this.PorcentajeIgv = (double )record["porcent_igv"];
            this.Igv = (double)record["val_igv"];
            this.SubTotalSinIgv = (double ) record["val_venta"];
            this.Flete = (double )record["flete"];
            this.Peso = (double)record["peso"];
            this.Descuento = (double ) record["dstco"];
            this.PorcentajeDescuento = (double )record["porcent1"];
            this.FechaRegistro = (DateTime )record["fec_registro"];
            this.EstadoRow = EnumRowState .NoModificar ;
        }

        #region Propiedades
        
        public int Item { get; set; }
        public int IdSucursal { get; set; }
        public string NumeroFactura { get; set; }
        public int IdDireccion { get; set; }
        public string IdProveedor { get; set; }
        public DateTime FechaFactura { get; set; }
        public int IdUnidad { get; set; }
        public string IdProducto { get; set; }
        public int IdLista { get; set; }
        public string IdLote { get; set; }
        public int IdAlmacen { get; set; }
        public int IdPrecio { get; set; }
        public double Precio { get; private set; }
        public double Cantidad { get; set; }
        
        public double SubTotal
        {
            get
            {
                return this.dblSutTotal;
            }
            set
            {
                this.dblSutTotal = value;
                Igv = Math.Round(dblSutTotal * dblPorcentajeIgv, 2);
                this.SubTotalSinIgv = this.dblSutTotal - this.Igv;
            }
        }
        public double PorcentajeIgv
        {
            get
            {
                return dblPorcentajeIgv ;
            }
            set
            {
                if (value >= 0)
                {
                    if (value > 1)
                    {
                        dblPorcentajeIgv = value / 100;
                    }
                    else
                    {
                        dblPorcentajeIgv = value;
                    }
                    if (dblSubTotalSinIng == 0)
                    {
                        Igv = dblSutTotal * dblPorcentajeIgv;
                        SubTotalSinIgv = dblSutTotal - Igv;
                    }
                    else {
                        this.SubTotal= value + Math.Round((value * this.PorcentajeIgv), 3);
                        Igv = dblSutTotal - value;
                    }
                }
                else
                {
                    dblPorcentajeIgv = 0.0;
                }
            }
        }
        public double Igv { get; private set; }
        public double SubTotalSinIgv
        {
            get
            {
                return this.dblSubTotalSinIng ;
            }
            set
            {
                this.dblSubTotalSinIng = value;

                if (this.dblPorcentajeIgv == 0)
                {
                    this.dblSutTotal = value;
                }
                else
                {
                    if (dblSutTotal == 0)
                    {
                        this.dblSutTotal = value + Math.Round((value * this.PorcentajeIgv), 3);
                        Igv = dblSutTotal - value;
                    }
                }
                
                if (this.Cantidad > 0)
                {
                    this.Precio = Math.Round(this.SubTotalSinIgv / this.Cantidad, 3);
                }
            }
        }

        public string Almacen { get; set; }

        public string Unidad { get; set; }
        public string Descripcion { get; set; }
        public double Flete { get; set; }
        public double Peso { get; set; }
        public double Descuento
        {
            get
            {
                return dblDescuento ;
            }
            set
            {
                this.dblDescuento = value;
                if (dblDescuento > 0) {
                    this.PorcentajeDescuento = (this.dblDescuento * 100) / (this.dblSutTotal + this.dblDescuento);
                }else{
                    PorcentajeDescuento = 0.0;
                }
                
            }
        }
        public double PorcentajeDescuento { get; private set; }
        public double CostoFinal
        {
            get
            {
                return (this.Precio + this.Flete);
            }
        }
        public DateTime FechaRegistro { get; set; }

        public EnumRowState EstadoRow { get; private set; }
        #endregion

        #region Metodos
        internal void SetEstado(EnumRowState estato)
        {
            EstadoRow = estato;
        }

        #endregion

        #region Funciones
        public object[] ToObject()
        {
            object[] obj = null;
            obj = new object[]
            {
                this.IdPrecio,
                this.Item,
                this.Precio,
                this.Cantidad,
                this.SubTotal,
                this.SubTotalSinIgv,
                this.Descuento,
                this.Igv,
                this.CostoFinal,
                this.Flete,
                this.PorcentajeIgv,
                this.PorcentajeDescuento,
                this.FechaRegistro,
                this.Peso,
                this.IdAlmacen,
                this.IdUnidad,
                this.IdProducto,
                this.IdLista,
                this.IdLote,
                this.IdSucursal,
                this.NumeroFactura,
                this.IdDireccion,
                this.IdProveedor,
                this.FechaFactura
            };

            return obj;
        }
        #endregion
    }
}